
from unittest import TestCase
from asynctools.utils import Chunker


class ChunkerTest(TestCase):

    def test_init(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1], [2, 3], [4, 5, 6]], c)

    def test_append_list(self):
        c = Chunker()
        c.append([1])
        self.assertEquals([[1]], c)

    def test_append_iter(self):
        c = Chunker()
        c.append(iter([1]))
        self.assertEquals([[1]], c)

    def test_insert_list(self):
        c = Chunker()
        c.insert(0, [1])
        self.assertEquals([[1]], c)

    def test_insert_iter(self):
        c = Chunker()
        c.insert(0, iter([1]))
        self.assertEquals([[1]], c)

    def test_ichain(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([1, 2, 3, 4, 5, 6], list(c.ichain()))

    def test_chunk_by_0(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertRaises(ValueError, lambda: c.chunk(size=0))

    def test_chunk_empty_input_by_3(self):
        c = Chunker([])
        self.assertEquals([], c.chunk(size=3))

    def test_chunk_by_1(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1], [2], [3], [4], [5], [6]], c.chunk(size=1))

    def test_chunk_by_2(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1, 2], [3, 4], [5, 6]], c.chunk(size=2))

    def test_chunk_by_5(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1, 2, 3, 4, 5], [6]], c.chunk(size=5))

    def test_chunk_by_6(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1, 2, 3, 4, 5, 6]], c.chunk(size=6))

    def test_chunk_by_7(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1, 2, 3, 4, 5, 6]], c.chunk(size=7))

    def test_split_chunks_of_0(self):
        c = Chunker([])
        self.assertEquals([], c.split([]))


    def test_split_chunks_of_1(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1], [2, 3], [4, 5, 6]], c.split([[1], [2], [3], [4], [5], [6]]))

    def test_split_chunks_of_2(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1], [2, 3], [4, 5, 6]], c.split([[1, 2], [3, 4], [5, 6]]))

    def test_split_chunks_of_5(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1], [2, 3], [4, 5, 6]], c.split([[1, 2, 3, 4, 5], [6]]))

    def test_split_chunks_of_6(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1], [2, 3], [4, 5, 6]], c.split([[1, 2, 3, 4, 5, 6]]))

    def test_split_chunks_of_7(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1], [2, 3], [4, 5, 6]], c.split([[1, 2, 3, 4, 5, 6]]))


    def test_ichunk_by_0(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertRaises(ValueError, lambda: list(c.ichunk(size=0)))

    def test_ichunk_empty_input_by_3(self):
        c = Chunker([])
        self.assertEquals([], list(c.ichunk(size=3)))

    def test_ichunk_by_1(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1], [2], [3], [4], [5], [6]], list(c.ichunk(size=1)))

    def test_ichunk_by_2(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1, 2], [3, 4], [5, 6]], list(c.ichunk(size=2)))

    def test_ichunk_by_5(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1, 2, 3, 4, 5], [6]], list(c.ichunk(size=5)))

    def test_ichunk_by_6(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1, 2, 3, 4, 5, 6]], list(c.ichunk(size=6)))

    def test_ichunk_by_7(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1, 2, 3, 4, 5, 6]], list(c.ichunk(size=7)))

    def test_isplit_chunks_of_0(self):
        c = Chunker([])
        self.assertEquals([], list(c.isplit([])))


    def test_isplit_chunks_of_1(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1], [2, 3], [4, 5, 6]], list(c.isplit([[1], [2], [3], [4], [5], [6]])))

    def test_isplit_chunks_of_2(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1], [2, 3], [4, 5, 6]], list(c.isplit([[1, 2], [3, 4], [5, 6]])))

    def test_isplit_chunks_of_5(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1], [2, 3], [4, 5, 6]], list(c.isplit([[1, 2, 3, 4, 5], [6]])))

    def test_isplit_chunks_of_6(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1], [2, 3], [4, 5, 6]], list(c.isplit([[1, 2, 3, 4, 5, 6]])))

    def test_isplit_chunks_of_7(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([[1], [2, 3], [4, 5, 6]], list(c.isplit([[1, 2, 3, 4, 5, 6]])))


    def test_iprocess_chunks_of_0(self):
        c = Chunker([])
        self.assertEquals([], list(c.iprocess([])))


    def test_iprocess_chunks_of_1(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([['1'], ['2', '3'], ['4', '5', '6']], list(c.iprocess(lambda x: str(x[0]))))

    def test_iprocess_chunks_of_2(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([['1'], ['2', '3'], ['4', '5', '6']], list(c.iprocess(lambda x: [str(y) for y in x], size=2)))

    def test_iprocess_chunks_of_5(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([['1'], ['2', '3'], ['4', '5', '6']], list(c.iprocess(lambda x: [str(y) for y in x], size=5)))

    def test_iprocess_chunks_of_6(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([['1'], ['2', '3'], ['4', '5', '6']], list(c.iprocess(lambda x: [str(y) for y in x], size=6)))

    def test_iprocess_chunks_of_7(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([['1'], ['2', '3'], ['4', '5', '6']], list(c.iprocess(lambda x: [str(y) for y in x], size=7)))

    def test_process_chunks_of_1(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([['1'], ['2', '3'], ['4', '5', '6']], c.process(lambda x: str(x[0])))

    def test_process_chunks_of_2(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([['1'], ['2', '3'], ['4', '5', '6']], c.process(lambda x: [str(y) for y in x], size=2))

    def test_process_chunks_of_5(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([['1'], ['2', '3'], ['4', '5', '6']], c.process(lambda x: [str(y) for y in x], size=5))

    def test_process_chunks_of_6(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([['1'], ['2', '3'], ['4', '5', '6']], c.process(lambda x: [str(y) for y in x], size=6))

    def test_process_chunks_of_7(self):
        c = Chunker([[1], [2, 3], [4, 5, 6]])
        self.assertEquals([['1'], ['2', '3'], ['4', '5', '6']], c.process(lambda x: [str(y) for y in x], size=7))
